package com.hmy.es.config;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(EsProperties.class)
public class EsClientAutoConfiguration implements ApplicationContextAware {

    @Autowired
    private EsProperties esProperties;


    @Override
    public void setApplicationContext(ApplicationContext ctx) throws BeansException {
        DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory)ctx.getAutowireCapableBeanFactory();

        esProperties.getEsClientMap().forEach((k, v) -> {
            defaultListableBeanFactory.registerSingleton(k + "EsClient", v);
        });

    }
}
